package xcmg.device.web.warehouse;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.WarehouseInboundAddVO;
import xcmg.device.service.vo.warehouse.WarehouseInboundDetailVO;
import xcmg.device.service.vo.warehouse.WarehouseInboundVO;
import xcmg.device.service.warehouse.WarehouseInboundService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
@RequestMapping("/warehouseInbound")
public class WarehouseInboundController extends BasicController {
    @Autowired
    private WarehouseInboundService warehouseInboundService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加入库记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult warehouse(@RequestBody WarehouseInboundAddVO warehouseInboundAddVO)throws Exception{
        if(null == warehouseInboundAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = warehouseInboundService.add(warehouseInboundAddVO, getUserId(), getOrgId());
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("入库记录删除")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(warehouseInboundService.delete(Arrays.asList(ids), getOrgId()));
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id)throws Exception{
        return actionValueResult(warehouseInboundService.findByPK(id, "id", getOrgId()));
    }

    @RequestMapping(value = "/itemByOrder",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemByOrder(String order)throws Exception{
        if(order != null && order.startsWith("XGQR")){
            return new ActionResult(ErrorCode.Failure.getCode(), "期初入库单请到期初条码中打印");
        }
        return actionValueResult(warehouseInboundService.findByPK(order, "order", getOrgId()));
    }

    @RequestMapping(value = "/insitructionDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查找配送指令记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult insitructionDetail(WarehouseInboundDetailVO warehouseInboundDetailVO)throws Exception{
        if(warehouseInboundDetailVO==null|| StringUtil.isNullOrEmpty(warehouseInboundDetailVO.getDeliveryInstructionNo())){
            return actionResult(ErrorCode.Success, new ArrayList<>());
        }
        warehouseInboundDetailVO.setOrgId(getOrgId());
        return actionValueResult(warehouseInboundService.insitructionDetail(warehouseInboundDetailVO));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询入库记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<WarehouseInboundVO> condition)throws Exception {
        WarehouseInboundVO warehouseInboundVO =condition.getFilters();
        if(warehouseInboundVO == null){
            warehouseInboundVO = new WarehouseInboundVO();
        }
        warehouseInboundVO.setOrgId(getOrgId());
        condition.setFilters(warehouseInboundVO);
        PageCommonVO pageCommonVO= warehouseInboundService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ApiOperation("导出入库数据列表")
    @FastMappingInfo(needLogin = true)
    public void export(@RequestBody String[] ids)throws Exception {
        warehouseInboundService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部入库数据列表")
    @FastMappingInfo(needLogin = true)
    public void exportAll(WarehouseInboundVO condition)throws Exception {
        if(condition == null){
            condition = new WarehouseInboundVO();
        }
        condition.setOrgId(getOrgId());
        warehouseInboundService.exportAll(condition);
    }
}
